// Copyright (c) 2023 Presto Labs Pte. Ltd.
// Author: tianqiang

#pragma once

namespace coin2::exchange::bittrex::order_v3 {

inline std::optional<std::string> GetErrorCode(
    const rapidjson::GenericValue<rapidjson::UTF8<>>& json) {
  if (!json.IsObject() || !json.HasMember("code")) {
    return std::nullopt;
  }

  return json["code"].GetString();
}

inline std::optional<std::string> GetErrorMsg(
    const rapidjson::GenericValue<rapidjson::UTF8<>>& json) {
  if (!json.IsObject() || !json.HasMember("detail")) {
    return std::nullopt;
  }

  return json["detail"].GetString();
}

inline coin::proto::OrderErrorCode GetOrderErrorCode(const std::string& exchange_error_code) {
  using coin::proto::OrderErrorCode;

  if (exchange_error_code == "INSUFFICIENT_FUNDS") {
    return OrderErrorCode::SUBMIT_ORDER_INSUFFICIENT_FUND;
  } else if (exchange_error_code == "ORDER_DOES_NOT_EXIST") {
    return OrderErrorCode::ORDER_NOT_FOUND;
  } else if (exchange_error_code == "ORDER_NOT_OPEN") {
    return OrderErrorCode::ORDER_CANCELED_ALREADY;
  } else if (exchange_error_code == "POST_ONLY_NOT_MET") {
    // POST_ONLY_NOT_MET not in bittrex's api doc
    // and unable check with bittrex people
    return OrderErrorCode::POST_ONLY_REJECT;
  } else {
    return OrderErrorCode::ORDER_UNKNOWN_ERROR;
  }
}

}  // namespace coin2::exchange::bittrex::order_v3
